/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-05-31
 * Time: 15:13
 */

public class Test {

    private static boolean isLegalChar(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
    }

    /**
     * 判断是否为回文串
     * @param s 有效字符仅仅为数字和字母，不区分大小写
     * @return 结果
     */
    public static boolean isPalindrome(String s) {
        s = s.toLowerCase();
        char[] str = s.toCharArray();
        int left = 0;
        int right = str.length-1;
        while(left < right) {
            if(isLegalChar(str[left]) && isLegalChar(str[right]) && str[left]==str[right]) {
                left++;
                right--;
            } else {
                if(!isLegalChar(str[left]))
                    left++;
                if(!isLegalChar(str[right]))
                    right--;
            }
            if(isLegalChar(str[left]) && isLegalChar(str[right]) && str[left]!=str[right]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "ab; cd' cb;a";
        System.out.println(isPalindrome(str));

    }
}
